home *** CD-ROM | disk | FTP | other *** search
/ Champak 138 / Volume 138 Aug 19 2011 - Damaged.iso / Games / startroopers.swf / scripts / mochicrypt / Preloader.as < prev    next >
Text File  |  2011-08-19  |  12KB  |  400 lines

  1. package mochicrypt
  2. {
  3.    import flash.display.Loader;
  4.    import flash.display.MovieClip;
  5.    import flash.events.Event;
  6.    import flash.events.IOErrorEvent;
  7.    import flash.events.ProgressEvent;
  8.    import flash.net.LocalConnection;
  9.    import flash.net.URLRequest;
  10.    import flash.net.URLRequestMethod;
  11.    import flash.net.URLVariables;
  12.    import flash.utils.ByteArray;
  13.    import flash.utils.getDefinitionByName;
  14.    import flash.utils.getTimer;
  15.    
  16.    public dynamic class Preloader extends MovieClip
  17.    {
  18.       
  19.       private static const AD_BACKGROUND:Boolean = Config.getBool("adBackground",false);
  20.       
  21.       private static const AD_URL:String = Config.getString("adURL","http://x.mochiads.com/srv/1/");
  22.       
  23.       private static const PATCH_URL:String = Config.getString("patchURL","http://cdn.mochiads.com/patch.swf");
  24.       
  25.       private static const ID:String = Config.getString("id","test");
  26.       
  27.       private static const PAYLOAD_NAME:String = "mochicrypt.Payload";
  28.       
  29.       private static const VERSION:String = "2.3c";
  30.        
  31.       
  32.       private var payloadLoader:Loader;
  33.       
  34.       private var patchLoader:Loader;
  35.       
  36.       private var adDone:Boolean = false;
  37.       
  38.       private var theme:Theme;
  39.       
  40.       private var adServerControl:Boolean = false;
  41.       
  42.       private var adLoader:Loader;
  43.       
  44.       private var adDuration:int = 11000;
  45.       
  46.       private var payloadProgress:Number = 0;
  47.       
  48.       private var patchProgress:Number = 0;
  49.       
  50.       private var lastProgress:Number = 0;
  51.       
  52.       private var patchDisabled:Boolean = false;
  53.       
  54.       private var adStarted:int;
  55.       
  56.       private var patchDone:Boolean = false;
  57.       
  58.       private var payloadDone:Boolean = false;
  59.       
  60.       private var origFrameRate:Number = NaN;
  61.       
  62.       private var adComplete:Boolean = false;
  63.       
  64.       private var lc:LocalConnection;
  65.       
  66.       public function Preloader()
  67.       {
  68.          lc = new LocalConnection();
  69.          adLoader = new Loader();
  70.          patchLoader = new Loader();
  71.          adStarted = getTimer();
  72.          super();
  73.          loaderInfo.addEventListener(ProgressEvent.PROGRESS,progressHandler);
  74.          loaderInfo.addEventListener(Event.INIT,initHandler);
  75.          loaderInfo.addEventListener(Event.COMPLETE,completeHandler);
  76.          loaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
  77.       }
  78.       
  79.       private function patchProgressHandler(param1:ProgressEvent) : void
  80.       {
  81.          if(param1.bytesLoaded == param1.bytesTotal)
  82.          {
  83.             patchProgress = NaN;
  84.          }
  85.          else
  86.          {
  87.             patchProgress = Number(param1.bytesLoaded) / param1.bytesTotal;
  88.          }
  89.          progress();
  90.       }
  91.       
  92.       private function adIOErrorHandler(param1:IOErrorEvent) : void
  93.       {
  94.          adDone = true;
  95.          finish();
  96.       }
  97.       
  98.       private function makeLCClient() : Object
  99.       {
  100.          var obj:Object = {};
  101.          obj.unloadAd = function():void
  102.          {
  103.             trace("unloadAd");
  104.             adDone = true;
  105.             finish();
  106.          };
  107.          obj.adSkipped = function():void
  108.          {
  109.             trace("adSkipped");
  110.             adDone = true;
  111.             finish();
  112.          };
  113.          obj.adLoaded = function(param1:Number, param2:Number):void
  114.          {
  115.             trace("adLoaded called");
  116.             theme.adLoaded(param1,param2);
  117.          };
  118.          obj.adjustProgress = function(param1:Number):void
  119.          {
  120.             trace("adjustProgress called");
  121.             adServerControl = true;
  122.             adStarted = getTimer();
  123.             adDuration = param1;
  124.          };
  125.          obj.adjustFrameRate = this.adjustFrameRate;
  126.          obj.disablePatch = function():void
  127.          {
  128.             patchDisabled = true;
  129.          };
  130.          return obj;
  131.       }
  132.       
  133.       private function finish() : void
  134.       {
  135.          var i:uint = 0;
  136.          var j:uint = 0;
  137.          var k:uint = 0;
  138.          var n:uint = 0;
  139.          var u:uint = 0;
  140.          var v:uint = 0;
  141.          if(!payloadDone || !adDone || !patchDone)
  142.          {
  143.             return;
  144.          }
  145.          if(payloadLoader)
  146.          {
  147.             trace("unexpected call to finish()");
  148.             return;
  149.          }
  150.          var payloadClass:Class = Class(getDefinitionByName(PAYLOAD_NAME));
  151.          var data:ByteArray = ByteArray(new payloadClass());
  152.          trace("data.length",data.length);
  153.          var S:ByteArray = new ByteArray();
  154.          n = data.length - 16;
  155.          i = 0;
  156.          while(i < 256)
  157.          {
  158.             S.writeByte(i);
  159.             i++;
  160.          }
  161.          j = 0;
  162.          i = 0;
  163.          while(i < 256)
  164.          {
  165.             j = j + S[i] + data[n + (i & 15)] & 255;
  166.             u = S[i];
  167.             S[i] = S[j];
  168.             S[j] = u;
  169.             i++;
  170.          }
  171.          if(n > 131072)
  172.          {
  173.             n = 131072;
  174.          }
  175.          i = j = uint(0);
  176.          k = 0;
  177.          while(k < n)
  178.          {
  179.             i = i + 1 & 255;
  180.             u = S[i];
  181.             j = j + u & 255;
  182.             v = S[j];
  183.             S[i] = v;
  184.             S[j] = u;
  185.             data[k] ^= S[u + v & 255];
  186.             k++;
  187.          }
  188.          data.uncompress();
  189.          try
  190.          {
  191.             data = patchLoader.content["patch"](data);
  192.          }
  193.          catch(error:Error)
  194.          {
  195.             trace("patch failed",error);
  196.          }
  197.          try
  198.          {
  199.             if(!isNaN(origFrameRate))
  200.             {
  201.                stage.frameRate = origFrameRate;
  202.             }
  203.          }
  204.          catch(error:Error)
  205.          {
  206.             trace("resetting frameRate failed",error);
  207.          }
  208.          payloadLoader = new Loader();
  209.          addChild(payloadLoader);
  210.          payloadLoader.loadBytes(data);
  211.          patchLoader.unload();
  212.          adLoader.unload();
  213.          trace("ad should be gone...");
  214.          removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
  215.          loaderInfo.removeEventListener(ProgressEvent.PROGRESS,progressHandler);
  216.          loaderInfo.removeEventListener(Event.INIT,initHandler);
  217.          loaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
  218.          loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
  219.          patchLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,patchIOErrorHandler);
  220.          patchLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,patchProgressHandler);
  221.          patchLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE,patchCompleteHandler);
  222.          adLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,adIOErrorHandler);
  223.          lc.close();
  224.          removeChild(theme);
  225.          theme.removeChild(adLoader);
  226.          lc = null;
  227.          adLoader = null;
  228.          patchLoader = null;
  229.          theme = null;
  230.       }
  231.       
  232.       private function enterFrameHandler(param1:Event) : void
  233.       {
  234.          progress();
  235.          if(!payloadDone && currentFrame == 2)
  236.          {
  237.             payloadDone = true;
  238.             finish();
  239.          }
  240.       }
  241.       
  242.       private function initHandler(param1:Event) : void
  243.       {
  244.          var event:Event = param1;
  245.          try
  246.          {
  247.             origFrameRate = stage.frameRate;
  248.          }
  249.          catch(error:Error)
  250.          {
  251.             trace("can\'t access stage.frameRate in initHandler",error);
  252.          }
  253.          adjustFrameRate(30);
  254.          var lcName:String = ["",Math.floor(new Date().getTime()),Math.floor(Math.random() * 999999)].join("_");
  255.          lc.client = makeLCClient();
  256.          lc.allowDomain("*","x.mochiads.com");
  257.          lc.allowInsecureDomain("*","x.mochiads.com");
  258.          lc.connect(lcName);
  259.          adLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,adIOErrorHandler);
  260.          adLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,adCompleteHandler);
  261.          var lv:URLVariables = new URLVariables();
  262.          lv.id = ID;
  263.          lv.res = "" + loaderInfo.width + "x" + loaderInfo.height;
  264.          lv.method = "showPreloaderAd";
  265.          lv.swfv = loaderInfo.swfVersion;
  266.          lv.mav = VERSION;
  267.          lv.lc = lcName;
  268.          lv.st = getTimer();
  269.          lv.as3_swf = loaderInfo.loaderURL;
  270.          lv.no_bg = !AD_BACKGROUND;
  271.          var req:URLRequest = new URLRequest(AD_URL + ID + ".swf");
  272.          req.contentType = "application/x-www-form-urlencoded";
  273.          req.method = URLRequestMethod.POST;
  274.          req.data = lv;
  275.          adLoader.x = 0.5 * loaderInfo.width;
  276.          adLoader.y = 0.5 * loaderInfo.height;
  277.          try
  278.          {
  279.             adLoader.load(req);
  280.          }
  281.          catch(error:Error)
  282.          {
  283.             adDone = true;
  284.          }
  285.          patchLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,patchIOErrorHandler);
  286.          patchLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,patchProgressHandler);
  287.          patchLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,patchCompleteHandler);
  288.          try
  289.          {
  290.             patchLoader.load(new URLRequest(PATCH_URL));
  291.          }
  292.          catch(error:Error)
  293.          {
  294.             patchDone = true;
  295.          }
  296.          theme = new Theme(adLoader,loaderInfo.width,loaderInfo.height);
  297.          addChild(theme);
  298.          addEventListener(Event.ENTER_FRAME,enterFrameHandler);
  299.          finish();
  300.       }
  301.       
  302.       private function progressHandler(param1:ProgressEvent) : void
  303.       {
  304.          if(param1.bytesLoaded == param1.bytesTotal)
  305.          {
  306.             payloadProgress = NaN;
  307.          }
  308.          else
  309.          {
  310.             payloadProgress = Number(param1.bytesLoaded) / param1.bytesTotal;
  311.          }
  312.          progress();
  313.       }
  314.       
  315.       private function adjustFrameRate(param1:Number) : void
  316.       {
  317.          var newFrameRate:Number = param1;
  318.          try
  319.          {
  320.             if(!isNaN(origFrameRate))
  321.             {
  322.                stage.frameRate = newFrameRate;
  323.             }
  324.          }
  325.          catch(error:Error)
  326.          {
  327.             trace("couldn\'t adjust stage.frameRate",error);
  328.          }
  329.       }
  330.       
  331.       private function ioErrorHandler(param1:IOErrorEvent) : void
  332.       {
  333.       }
  334.       
  335.       private function patchCompleteHandler(param1:Event) : void
  336.       {
  337.          patchDone = true;
  338.          finish();
  339.       }
  340.       
  341.       private function progress() : void
  342.       {
  343.          var _loc1_:Number = NaN;
  344.          _loc1_ = 1;
  345.          if(!adDone)
  346.          {
  347.             _loc1_ = Number(getTimer() - adStarted) / adDuration;
  348.          }
  349.          if(!payloadDone && !isNaN(payloadProgress) && payloadProgress < _loc1_)
  350.          {
  351.             _loc1_ = payloadProgress;
  352.          }
  353.          if(!patchDone && !isNaN(patchProgress) && patchProgress < _loc1_)
  354.          {
  355.             _loc1_ = patchProgress;
  356.          }
  357.          if(_loc1_ > 1)
  358.          {
  359.             _loc1_ = 1;
  360.          }
  361.          if(_loc1_ > lastProgress)
  362.          {
  363.             lastProgress = _loc1_;
  364.             theme.updateProgress(lastProgress);
  365.          }
  366.          if(!adServerControl && getTimer() > adStarted + adDuration)
  367.          {
  368.             trace("giving up... maybe?");
  369.             if(!adComplete)
  370.             {
  371.                adLoader.close();
  372.             }
  373.             adDone = true;
  374.             finish();
  375.          }
  376.       }
  377.       
  378.       private function patchIOErrorHandler(param1:IOErrorEvent) : void
  379.       {
  380.          patchDone = true;
  381.          finish();
  382.       }
  383.       
  384.       private function adCompleteHandler(param1:Event) : void
  385.       {
  386.          if(!adServerControl)
  387.          {
  388.             adStarted = getTimer();
  389.          }
  390.          adComplete = true;
  391.          progress();
  392.       }
  393.       
  394.       private function completeHandler(param1:Event) : void
  395.       {
  396.          nextFrame();
  397.       }
  398.    }
  399. }
  400.